import { message } from 'antd';
import {
  getOrgList,
  getOrgRootTree,
  getChildOrgListByParentId,
  deleteOrg,
  addOrg,
  updateOrg,
  getPositionList,
  addPosition,
  delPosition,
  setMainPosition,
  getAllPositionList,
  getListDistinctPositionNames,
} from '../services';

export default {
  namespace: 'orgManageModel',
  state: {
    orgSelectList: [],
    orgSelectListTotal: 0,
  },
  effects: {
    *getOrgList({ payload }, { put, call, select }) {
      const res = yield call(getOrgList, payload);
      const { code, data, message: errMsg } = res;
      if (code === 0) {
        const { dataList, total } = data;
        yield put({
          type: 'updateModel',
          payload: {
            orgSelectList: dataList,
            orgSelectListTotal: total,
          },
        });
      } else {
        message.error(errMsg);
      }
    },
    *getOrgListOfDetailPage({ payload }, { put, call, select }) {
      return yield call(getOrgList, payload);
    },
    *getOrgRootTree(_, { put, call, select }) {
      const res = yield call(getOrgRootTree);
      const { code, data } = res;
      if (code === 0 && Array.isArray(data)) {
        // const str = JSON.stringify(data).replace(/orgName/g, 'title')
        //   .replace(/orgCode/g, 'key');
        return data.map((d) => ({
          ...d,
          title: d.orgName,
          key: d.orgCode,
          value: d.id,
        }));
      }
      message.error('获取根组织失败');
    },
    *getChildOrgListByParentId({ payload }, { put, call, select }) {
      const res = yield call(getChildOrgListByParentId, payload);
      const { code, data } = res;
      if (code === 0 && Array.isArray(data)) {
        // const str = JSON.stringify(data).replace(/orgName/g, 'title')
        //   .replace(/orgCode/g, 'key');
        // return JSON.parse(str);
        return data.map((d) => ({
          ...d,
          title: d.orgName,
          key: d.orgCode,
        }));
      }
      message.error('获取子组织失败');
    },
    *deleteOrg({ payload }, { put, call, select }) {
      return yield call(deleteOrg, payload);
    },
    *addOrg({ payload }, { put, call, select }) {
      return yield call(addOrg, payload);
    },
    *updateOrg({ payload }, { put, call, select }) {
      return yield call(updateOrg, payload);
    },
    *getPositionList({ payload }, { put, call, select }) {
      const res = yield call(getPositionList, payload);
      const { code, data, message: errMsg } = res;
      if (code !== 0) {
        message.error(errMsg);
        return null;
      }
      return data;
    },
    *getAllPositionList({ payload }, { put, call, select }) {
      const res = yield call(getAllPositionList, payload);
      const { code, data, message: errMsg } = res;
      if (code !== 0) {
        message.error(errMsg);
        return null;
      }
      return data;
    },
    *getListDistinctPositionNames({ payload }, { put, call, select }) {
      const res = yield call(getListDistinctPositionNames, payload);
      const { code, data, message: errMsg } = res;
      if (code !== 0) {
        message.error(errMsg);
        return null;
      }
      return data;
    },
    *addPosition({ payload }, { put, call, select }) {
      return yield call(addPosition, payload);
    },
    *delPosition({ payload }, { put, call, select }) {
      return yield call(delPosition, payload);
    },
    *setMainPosition({ payload }, { put, call, select }) {
      return yield call(setMainPosition, payload);
    },
  },
  reducers: {
    /* 更新状态（通用） */
    updateModel: (state, { payload }) => ({
      ...state,
      ...payload,
    }),
  },
};
